package com.onesports.intelligent.k12.polarlight.service.sys;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.onesports.intelligent.k12.polarlight.constant.BizConstants;
import com.onesports.intelligent.k12.polarlight.domain.entity.organization.Organization;
import com.onesports.intelligent.k12.polarlight.domain.entity.organization.OrganizationCampus;
import com.onesports.intelligent.k12.polarlight.domain.entity.sys.SysDataScope;
import com.onesports.intelligent.k12.polarlight.domain.vo.sys.SysDataScopeSelectVO;
import com.onesports.intelligent.k12.polarlight.enums.DataScopeTypeEnum;
import com.onesports.intelligent.k12.polarlight.mapper.organization.OrganizationCampusMapper;
import com.onesports.intelligent.k12.polarlight.mapper.organization.OrganizationMapper;
import com.onesports.intelligent.k12.polarlight.mapper.sys.SysDataScopeMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.compress.utils.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author wjj
 * @author aj
 */
@Service
@RequiredArgsConstructor
public class DataScopeService extends ServiceImpl<SysDataScopeMapper, SysDataScope> {

    private final OrganizationMapper organizationMapper;

    private final OrganizationCampusMapper organizationCampusMapper;

    private final SysDataScopeMapper sysDataScopeMapper;

    /**
     * 根据角色id获取角色数据权限
     *
     * @param roleId 角色id
     * @return List<SysDataScope>
     */
    public List<SysDataScope> getSysDataScopeByRoleId(String roleId) {
        LambdaQueryWrapper<SysDataScope> roleMenuQuery = new QueryWrapper<SysDataScope>().lambda()
                .eq(SysDataScope::getRoleId, roleId);
        return baseMapper.selectList(roleMenuQuery);
    }

    /**
     * 根据角色id获取角色数据权限
     *
     * @param roleIds 角色id
     * @return List<SysDataScope>
     */
    public List<SysDataScope> getSysDataScopeByRoleId(List<String> roleIds) {
        return baseMapper.selectList(new QueryWrapper<SysDataScope>().lambda().in(SysDataScope::getRoleId, roleIds));
    }

    /**
     * 根据机构ID获取不同的数据权限表
     *
     * @param organizationId 机构ID
     * @return
     */
    public List<SysDataScopeSelectVO> getDataByRoleId(String organizationId) {
        List<SysDataScopeSelectVO> list = Lists.newArrayList();
        if (StringUtils.isBlank(organizationId)) {
            return list;
        }

        if (BizConstants.OPERATION_ORGANIZATION_ID.equals(organizationId)) {
            //获取机构数据
            List<Organization> organizationList = organizationMapper.selectList(new QueryWrapper<Organization>().lambda()
                    .eq(Organization::getDeleted, false));
            if (CollectionUtils.isNotEmpty(organizationList)) {
                list = organizationList.stream().map(
                        organization -> {
                            SysDataScopeSelectVO scopeSelectVO = new SysDataScopeSelectVO();
                            scopeSelectVO.setId(organization.getId());
                            scopeSelectVO.setLabel(organization.getOrganizationName());
                            return scopeSelectVO;
                        }
                ).collect(Collectors.toList());
            }
        } else {
            LambdaQueryWrapper<OrganizationCampus> wrapper = new LambdaQueryWrapper<OrganizationCampus>()
                    .eq(OrganizationCampus::getDeleted, false)
                    .eq(OrganizationCampus::getOrganizationId, organizationId);
            //获取校区数据
            List<OrganizationCampus> campuses = organizationCampusMapper.selectList(wrapper);
            if (CollectionUtils.isNotEmpty(campuses)) {
                list = campuses.stream().map(campuse -> {
                    SysDataScopeSelectVO scopeSelectVO = new SysDataScopeSelectVO();
                    scopeSelectVO.setId(campuse.getId());
                    scopeSelectVO.setLabel(campuse.getCampusName());
                    return scopeSelectVO;
                }).collect(Collectors.toList());
            }
        }
        return list;
    }

    /**
     * 查询数据权限(校区)
     *
     * @param accountId
     * @return
     */
    public List<String> listDataScopeWithCampus(String accountId) {
        return listDataScope(accountId, DataScopeTypeEnum.CAMPUS);
    }

    /**
     * 查询数据权限(机构)
     *
     * @param accountId
     * @return
     */
    public List<String> listDataScopeWithOrg(String accountId) {
        return listDataScope(accountId, DataScopeTypeEnum.ORGANIZATION);
    }

    public List<String> listDataScope(String accountId, DataScopeTypeEnum dataScopeType) {
        if (StringUtils.isEmpty(accountId)) {
            return null;
        }
        List<SysDataScope> sysDataScopes = sysDataScopeMapper.listByAccountId(accountId, dataScopeType);
        if (CollectionUtils.isNotEmpty(sysDataScopes)) {
            return sysDataScopes.stream().map(SysDataScope::getTargetId).collect(Collectors.toList());
        }
        return null;
    }
}
